package com.xu.book.dao.impl;

import com.xu.book.bean.Book;
import com.xu.book.bean.BorrowCard;
import com.xu.book.dao.IBorrowCardDao;
import com.xu.sys.bean.SysUser;
import com.xu.sys.utils.MyDbutils;
import com.xu.sys.utils.PageUtils;
import com.xu.sys.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BorrowCardDaoimpl implements IBorrowCardDao {
    @Override
    public List<BorrowCard> listPage(PageUtils pageUtils,SysUser user) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_borrow_card where 1 = 1";
        System.out.println(user.getIsAdmin());
        if(StringUtils.isNotEmpty(pageUtils.getKey())){
            sql += " and stuname like '%"+pageUtils.getKey()+"%' ";
        }
        if(user != null && user.getIsAdmin() == false){
            // 不是管理员
            sql += " and userid = " + user.getId();
            System.out.println(sql);
        }
        sql += " limit ?,? ";
        // 计算 分页开始的位置
        int startNo = pageUtils.getStart();
        try {
            return queryRunner.query(sql
                    ,new BeanListHandler<BorrowCard>
                            (BorrowCard.class),startNo,pageUtils.getPageSize());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int count(PageUtils pageUtils, SysUser user) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select count(1) from t_borrow_card where 1 = 1 ";
        if(StringUtils.isNotEmpty(pageUtils.getKey())){
            sql += " and stuname like '%"+pageUtils.getKey()+"%' ";
        }
        if(user != null && user.getIsAdmin() == false){
        // 不是管理员
            sql += " and userid = " + user.getId();
        }
        try {
            return queryRunner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    return resultSet.getInt(1);
                }
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<BorrowCard> list() {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_borrow_card  ";
        try {
            return queryRunner.query(sql, new BeanListHandler<BorrowCard>(BorrowCard.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }



    @Override
    public int save(BorrowCard entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "insert into t_borrow_card(stuid,userid,stuname,state,starttime,endtime)value(?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql,entity.getStuid(),entity.getUserID(),entity.getStuname(),
                    entity.getState(),entity.getStarttime(),entity.getEndtime());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateById(BorrowCard entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "update t_borrow_card set stuid=?,stuname=?,userid=?,state=?,starttime=?,endtime = ? where id = ?";
        try {
            return queryRunner.update(sql,entity.getStuid(),entity.getStuname(),entity.getUserID(),
                    entity.getState(),entity.getStarttime(),entity.getEndtime(),entity.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public BorrowCard findById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_borrow_card where id = ?";
        try {
            List<BorrowCard> list = queryRunner.query(sql, new BeanListHandler<BorrowCard>(BorrowCard.class), id);
            if (list !=null && list.size() >0){
                return list.get(0);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql ="delete from t_borrow_card where id = ?";
        try {
            return queryRunner.update(sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<BorrowCard> listCanuseCard(Integer userId) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_borrow_card where userid = ? and state = 0 ";
        try {
            return queryRunner.query(sql, new BeanListHandler<BorrowCard>(BorrowCard.class),userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
